/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.pb138.srgsxmldb.fileUtils;

import cz.muni.fi.pb138.srgsxmldb.servlets.MyErrorListener;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import org.apache.commons.fileupload.FileItem;

/**
 * Class providing useful methods to work with files
 * @author Eldred
 */
public class FileUtil {
    

    /**
     * copy file to file
     * @param in file item to copy from
     * @param out output stream to be copied to
     */
    public static void copy(FileItem in, OutputStream out) {
        try {
            
            byte[] bArray=in.get();
            out.write(bArray); 
            out.flush();
            out.close();
            
        } catch (IOException ex) {
            Logger.getLogger(FileUtil.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    /**
     * get from path name of the file
     * @param fullName path
     * @return file name only
     */
    public static String extractFileName (String fullName) {  
        
        Pattern p = Pattern.compile(".*?([^\\\\/]+)$");  
        Matcher m = p.matcher(fullName);  

        return (m.find()) ? m.group(1) : "";  
    } 
    
    /**
     * delete file safely
     * @param filename file to delete
     * @return 
     */
    public static boolean deleteFile(String filename) {
        
        String fileName = filename;
        // A File object to represent the filename
        File f = new File(fileName);

        // Make sure the file or directory exists and isn't write protected
        if (!f.exists())
        throw new IllegalArgumentException(
            "Delete: no such file or directory: " + fileName);

        if (!f.canWrite())
        throw new IllegalArgumentException("Delete: write protected: "
            + fileName);

        // If it is a directory, make sure it is empty
        if (f.isDirectory()) {
        String[] files = f.list();
        if (files.length > 0)
            throw new IllegalArgumentException(
                "Delete: directory not empty: " + fileName);
        }

        // Attempt to delete it
        boolean success = f.delete();

        if (!success)
            throw new IllegalArgumentException("Delete: deletion failed");
        
        return success; 
    }
    
    /**
     * transform xml to txt by xslt
     * @param inXML file to transform from
     * @param inXSL file to transform by
     * @param outTXT file to transform to
     * @throws TransformerConfigurationException
     * @throws TransformerException 
     */
    public static void transform(String inXML,String inXSL,String outTXT)
    throws TransformerConfigurationException, TransformerException {

        TransformerFactory factory = TransformerFactory.newInstance();

        StreamSource xslStream = new StreamSource(inXSL);
        Transformer transformer = factory.newTransformer(xslStream);
        transformer.setErrorListener(new MyErrorListener());

        StreamSource in = new StreamSource(inXML);
        StreamResult out = new StreamResult(outTXT);
        transformer.transform(in,out);
    }
}
